﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Krile.Forms.MainForm.Controls
{
    public partial class SearchBar : UserControl
    {
        /// <summary>
        /// Selected index changed.
        /// </summary>
        public event EventHandler OnSelectedIndexChanged;

        /// <summary>
        /// Needle changed.
        /// </summary>
        public event EventHandler OnNeedleChanged;

        public event EventHandler OnCloseRequired;

        public event EventHandler OnCreateNewTabRequired;

        /// <summary>
        /// Needle string
        /// </summary>
        public string Needle
        {
            get { return MainInput.Text; }
            set { MainInput.Text = value; }
        }

        private int _foundNum;
        /// <summary>
        /// Set found items num.
        /// </summary>
        public int FoundNum
        {
            get { return _foundNum; }
            set
            {
                _foundNum = value;
                if (selectedIndex >= FoundNum)
                {
                    SelectedIndex = FoundNum - 1;
                }
                UpdateResult();
            }
        }

        private int selectedIndex;
        /// <summary>
        /// Selected index
        /// </summary>
        public int SelectedIndex
        {
            get { return selectedIndex; }
            set
            {
                if (value < 0) value = 0;
                if (value > FoundNum) value = FoundNum;
                selectedIndex = value;
                SpinSelector.Position = value;
                UpdateResult();
            }
        }

        public SearchBar()
        {
            InitializeComponent();
            UpdateResult();
        }

        private void InnerPanel_Click(object sender, EventArgs e)
        {
            MainInput.Focus();
        }

        private void SearchIconPict_Click(object sender, EventArgs e)
        {
            MainInput.Focus();
        }

        private void UpdateResult()
        {
            if (FoundNum == 0)
            {
                ResultPanel.BackColor = Color.Red;
                ResultLabel.ForeColor = Color.White;
                ResultLabel.Text = "0 / 0";
                SpinSelector.Enabled = false;
            }
            else
            {
                ResultPanel.BackColor = Color.SkyBlue;
                ResultLabel.ForeColor = Color.Black;
                ResultLabel.Text = (selectedIndex + 1).ToString() + " / " + FoundNum.ToString();
                SpinSelector.Enabled = true;
                SpinSelector.MaxValue = FoundNum - 1;
                SpinSelector.Position = selectedIndex;
            }
            ResultPanel.Width = ResultLabel.Width + SpinSelector.Width + 6;
        }

        private void MainInput_TextChanged(object sender, EventArgs e)
        {
            ResultPanel.Visible = MainInput.Text != String.Empty;
            if (OnNeedleChanged != null)
                OnNeedleChanged.Invoke(this, new EventArgs());
        }

        private void SplitSelector_Click(object sender, EventArgs e)
        {
            selectedIndex = SpinSelector.Position;
            if (OnSelectedIndexChanged != null)
                OnSelectedIndexChanged.Invoke(this, new EventArgs());
        }

        private void AddTab_Click(object sender, EventArgs e)
        {
            if (OnCreateNewTabRequired != null)
                OnCreateNewTabRequired.Invoke(this, e);
        }

        private void Closer_Click(object sender, EventArgs e)
        {
            if (OnCloseRequired != null)
                OnCloseRequired.Invoke(this, e);
        }

        private void SearchBar_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                e.IsInputKey = true;
            }
        }

        private void SearchBar_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                e.Handled = true;
                if (OnCloseRequired != null)
                    OnCloseRequired.Invoke(this, new EventArgs());
            }
        }
    }
}
